package com.neuedu.learn;

public class DemoMethods {


    /***
     *   public static void method(int end){
     * @param end 终止变量
     */
    public static void method(int end){
        if(end <= 0){
            System.out.println("终止数字非法");
            return ;
        }
        for (int i = 0; i < end; i++) {
            System.out.println("i = " + i);
        }
    }

    /**
     *  numa , numb 形参
     * @param numa
     * @param numb
     * @return 加和
     *
     *   方法重载的规则 方法名称相同 方法的参数必须不同 参数个数不同 或 参数类型不同 方法的返回值类型可以相同，也可以不同
     *
     */
    public static int sum(int numa , int numb){
        System.out.println("DemoMethods.sum  1 调用");
        int result = numa + numb;
        return result;
    }

    public static double sum(int numa , double numb){
        System.out.println("DemoMethods.sum 2 调用");
        double result = numa + numb;
        return result;
    }
    public static double sum(int numa , double numb ,int numc){
        double result = numa + numb + numc;
        return result;
    }


    /**
     *
     * @param numa
     * @param numb
     * @param numothers 数组  可变长参数 放到方法列表的末尾
     * @return
     */
    public static  int sum(int numa , int numb ,  int... numothers){
        int result = numa  + numb ;
        //numothers 数组
        if(numothers != null && numothers.length>0){
            for (int numother : numothers) {
                result += numother;
            }
        }

        return result ;
    }


    /**
     * 方法的递归调用  求某一个数的阶乘
     *
     *   jc(5) =   5 *   ......    1
     *
     *       *  1
     *
     * @param num
     * @return
     */
    public static int jc(int num){

        if(  num == 1 ){
            return 1;
        }
        return num * jc(num - 1);
    }


    public static void main(String[] args) {
        method(-1);

        double result = sum(10,20);
        System.out.println("result = " + result);

        result = sum(10,20.0);
        System.out.println("result = " + result);

        result = sum(10,20, 50,20 );
        System.out.println("result = " + result);

        //jdk中的例子
        System.out.println(String.format("姓名:%s ,年龄 :%d", "张飞",50));

        //调用递归方法
        result = jc(5);


    }


}
